package io.renren.common.utils;

import com.alibaba.fastjson.JSONObject;
import io.renren.config.WxConfig;
import io.renren.modules.shop.entity.WxEntity;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Log4j2
@Component
public class WxUtils {
    @Value("$(wx.appId)")
    private static String appId;

    @Value("$(wx.appSecret)")
    private static String secret;

    public String token;

    public static WxEntity checkWx(String js_code) {
        // 连接wx服务器的url地址
        String token_url = WxConfig.LOGIN_URL + "?appid=" + WxConfig.APPId + "&secret=" + WxConfig.secret + "&js_code=" + js_code + "&grant_type=authorization_code";
        log.info("这是网址" + token_url);
        WxEntity wxEntity = new WxEntity();
        try {
            HttpClient client = HttpClientBuilder.create().build(); // 构建一个httpClient
            HttpGet get = new HttpGet(token_url); //构建一个get请求
            HttpResponse httpResponse = client.execute(get); // 提交get请求
            HttpEntity httpResponseEntity = httpResponse.getEntity(); // 拿到返回的httpResponse的实体
            String content = EntityUtils.toString(httpResponseEntity);
            wxEntity = JSONObject.parseObject(content, WxEntity.class); // 将获得的json数据转化为实体类返回
            log.info("这是实体类数据");
            log.info(content);
            log.info(wxEntity);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return wxEntity;
    }


}
